package com.angis.fx.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class QuickTextDBHelper extends ObjectDBHelper {

	public static final String PK_QID = "qid";
	public static final String COLUMN_DETAIL = "details";
	public static final String COLUMN_QCREATIONTIME = "qcreationtime";
	public static final String COLUMN_CREATIONUSER = "creationuser";
	private static final String TABLE_QUICKTEXT = "quicktext";

	public QuickTextDBHelper(Context context) {
		super(context);
	}

	public long insertQuickText(String pQid, String pDetails, String pQcreationtme,
			String pCreationuser) {
		ContentValues lInitialValues = new ContentValues();
		lInitialValues.put(PK_QID, pQid);
		lInitialValues.put(COLUMN_DETAIL, pDetails);
		lInitialValues.put(COLUMN_QCREATIONTIME, pQcreationtme);
		lInitialValues.put(COLUMN_CREATIONUSER, pCreationuser);
		return insert(TABLE_QUICKTEXT, lInitialValues);
	}

	public boolean deleteQuickText(String pQid) {
		String params[] = new String[] { pQid };
		if(null == writeDB) {
			writeDB = dbHelper.getWritableDatabase();
		}
		return writeDB.delete(TABLE_QUICKTEXT, PK_QID + "=?", params) > 0;
	}

	public boolean updateQuickText(String pQid, String pDetails,
			String pQcreationtme, String pCreationuser) {
		String params[] = new String[] { pQid };
		ContentValues lInitialValues = new ContentValues();
		if (null != pDetails) {
			lInitialValues.put(COLUMN_DETAIL, pDetails);
		}
		if (null != pQcreationtme) {
			lInitialValues.put(COLUMN_QCREATIONTIME, pQcreationtme);
		}
		if (null != pCreationuser) {
			lInitialValues.put(COLUMN_CREATIONUSER, pCreationuser);
		}
		if(null == writeDB) {
			writeDB = dbHelper.getWritableDatabase();
		}
		return writeDB.update(TABLE_QUICKTEXT, lInitialValues, PK_QID + "=?",
				params) > 0;
	}

	public Cursor getAllQuickText() {
		if(null == readDB) {
			readDB = dbHelper.getReadableDatabase();
		}
		return readDB.query(TABLE_QUICKTEXT, new String[] { PK_QID,
				COLUMN_DETAIL, COLUMN_QCREATIONTIME, COLUMN_CREATIONUSER }, null, null,
				null, null, null);
	}

	public Cursor getSingleQuickText(String pQid) {
		String params[] = new String[] { pQid };
		if(null == readDB) {
			readDB = dbHelper.getReadableDatabase();
		}
		return readDB.query(TABLE_QUICKTEXT, new String[] { PK_QID,
				COLUMN_DETAIL, COLUMN_QCREATIONTIME, COLUMN_CREATIONUSER }, PK_QID
				+ "=?", params, null, null, null);
	}

}
